<template>
  <div class="">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="w-[300px]">
          <a-input v-model:value="formState.zjm" placeholder="设备名称/助记码/装备编号/设备序列号" />
        </a-form-item>

        <a-form-item name="dateRange" label="调拨时间" class="w-[300px] ml-4">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" :allow-clear="false" value-format="YYYY-MM-DD" @change="data" />
        </a-form-item>

        <a-form-item name="status" label="" class="w-[300px] ml-4">
          <a-radio-group v-model:value="formState.status">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">未归还</a-radio>
            <a-radio value="2">已归还</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onCLickEdit"> 新增 </a-button>
        <BaseExport type="战救" name="装备调出" code="000599" :search-params="formState">导出</BaseExport>
        <!-- <a-button @click="onPrint">打印</a-button> -->
        <a-button v-print="{ id: 'printMe' }" class="ml-2">打印</a-button>
        <div class="absolute top-[-9999px] hidden">
          <PrintView id="printMe" :data-list="tableData" />
        </div>
      </div>
      <BaseTable :data-source="tableData" fill :loading="loading" column-code="000599" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'NO'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            {{ statusFun(record.status) }}
          </template>

          <template v-if="column.dataIndex === 'equipmentName'">
            <MedicineInfo :value="{ mc: record.equipmentName, gg: record.specifications, cj: record.manufacturer }" />
          </template>

          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton type="link" size="small" :disabled="record.status === '1'" @click="onCLickStatus(record)"> 归还 </BaseButton>
              <BaseButton confirm-text="确认删除？" type="link" danger size="small" :disabled="record.status === '1'" @click="onCLickDel(record)">
                删除
              </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData()" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'
import PrintView from './PrintView.vue'

const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  status: ''
})

const onCLickStatus = async (record) => {
  const res = await post('/war/equipmentTransfer/doUpdateEquipmentTransfer', {
    id: record.id,
    status: 1
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const statusFun = (status) => {
  if (status === '1') {
    return '已归还'
  } else {
    return '未归还'
  }
}
const onCLickDel = async (record) => {
  const res = await post('/war/equipmentTransfer/doDelEquipmentTransfer', {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
// 打印
// const onPrint = async () => {
//   const { code, msg, data } = await post('/inp/Print/hospitalizationNoticesPrint', {})
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }
const { loading, tableData, getData, pagination } = useTable({
  url: '/war/equipmentTransfer/getEquipmentTransferOutList',
  immediate: true,
  formatParams: () => {
    return {
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1],
      ...formState.value
    }
  }
})
const onCLickEdit = (record = {}) => {
  rowData.value = record
  editOpen.value = true
}

onMounted(() => {
  getData()
})
</script>
